﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sunimage.Spjg.BLL.SysMgr;
using Sunimage.Spjg.Common.SysMgr;
using Sunimage.Spjg.Web.Helper;

namespace Sunimage.Spjg.Web.Admin
{
    public partial class Roles : System.Web.UI.Page
    {
        RoleBl roleBl = new RoleBl();
        PermissionBl permBl = new PermissionBl();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //cblAdmin.DataSource = permBl.GetAllPermissions();
                //cblAdmin.DataTextField = "Name";
                //cblAdmin.DataValueField = "Id";
                //cblAdmin.DataBind();


                cblUser.DataSource = permBl.GetAllPermissions();
                cblUser.DataTextField = "Name";
                cblUser.DataValueField = "Id";
                cblUser.DataBind();


                LoadData();
            }
        }

        private void LoadData()
        {
            //Role roleAdmin = roleBl.GetRole(1);
            //foreach (ListItem item in cblAdmin.Items)
            //{
            //    if (roleAdmin.Permissions.Split(',').Contains(item.Value))
            //        item.Selected = true;
            //    else
            //        item.Selected = false;
            //}

            Role roleUser = roleBl.GetRole(2);
            foreach (ListItem item in cblUser.Items)
            {
                if (roleUser.Permissions.Split(',').Contains(item.Value))
                    item.Selected = true;
                else
                    item.Selected = false;
            }
        }


        protected void btnSaveNewUser_Click(object sender, EventArgs e)
        {
            //Role roleAdmin = roleBl.GetRole(1);
            //roleAdmin.Permissions = string.Empty;
            //for (int i = 0; i < cblAdmin.Items.Count; i++)
            //{
            //    ListItem item = cblAdmin.Items[i];
            //    if (item.Selected)
            //    {
            //        roleAdmin.Permissions += item.Value;
            //        roleAdmin.Permissions += ",";
            //    }
            //}
            //roleAdmin.Permissions = roleAdmin.Permissions.TrimEnd(',');

            //roleBl.Update(roleAdmin);

            Role roleUser = roleBl.GetRole(2);
            roleUser.Permissions = string.Empty;
            for (int i = 0; i < cblUser.Items.Count; i++)
            {
                ListItem item = cblUser.Items[i];
                if (item.Selected)
                {
                    roleUser.Permissions += item.Value;
                    roleUser.Permissions += ",";
                }
            }
            roleUser.Permissions = roleUser.Permissions.TrimEnd(',');

            roleBl.Update(roleUser);


            LoadData();

            MessageHelper.RegShowJSAjax(btnSaveNewUser, "修改权限成功", this.Page);
        }
    }
}